"""

def func1(*args):
    for arg in args:
        print(arg)

def func2(**kwargs):
    for key, value in kwargs.items():
        print(key, value)

func1(1, 2, 3)  # 输出: 1, 2, 3
func2(a=1, b=2, c=3)  # 输出: a 1, b 2, c 3

def add(a: int, b: int) -> int:
    return a + b

result = add(1,2)
print(type(result))

"""

# global：声明或定义全局变量（要么直接使用现有的全局作用域的变量，要么定义一个变量放到全局作用域）。
# x = 10
#
# def func():
#     global x
#     x = 20
#
# func()
# print(x)  # 输出: 20


# nonlocal：声明使用嵌套作用域的变量（嵌套作用域必须存在该变量，否则报错）。
# def outer():
#     x = 10
#     def inner():
#         nonlocal x
#         x = 20
#     inner()
#     print(x)  # 输出: 20
#
# outer()


# 使用闭包创建一个函数工厂，封装状态。
# def make_counter():
#     count = 0
#     def counter():
#         nonlocal count
#         count += 1
#         return count
#     return counter
#
# counter = make_counter()
# print(counter())  # 输出: 1
# print(counter())  # 输出: 2

# 示例：装饰器
# def my_decorator(func):
#     def wrapper():
#         print("Something is happening before")
#         func()
#         print("Something is happening after")
#     return wrapper
#
# @my_decorator
# def my_function():
#     print("Hello")
#
# my_function()

import time

# 定义装饰器
def timer(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"{func.__name__} executed in {end_time - start_time} seconds")
        return result
    return wrapper

# 使用装饰器
@timer
def example_function():
    time.sleep(2)  # 模拟耗时操作
    print("Function is running...")

example_function()